#include "batchnetworkaccessmanager.h"
#include "t_core/nbr.h"
////#include "env.h"

BatchNetworkAccessManager::BatchNetworkAccessManager(QObject *parent) :
    QNetworkAccessManager(parent)
{
}

QByteArray BatchNetworkAccessManager::doGet(QString a_url, QString *a_contentType)
{
    QUrl v_url;
    v_url.setUrl(a_url);
    return this->doGet(v_url, a_contentType);
}

QByteArray BatchNetworkAccessManager::doGet(QUrl a_url, QString *a_contentType)
{
    QNetworkRequest v_request;
    v_request.setUrl(a_url);
    return this->doGet(v_request, a_contentType);
}

QByteArray BatchNetworkAccessManager::doGet(QNetworkRequest request, QString *a_contentType)
{
    qDebug() << "doGet()" << request.url();
    QNetworkReply *v_reply = this->get(request);
    //NetworkWaiter *v_nbr = new NetworkWaiter(v_reply);
    T_NetworkWaiter v_nbr(v_reply);
    v_nbr.wait();
    QByteArray v_response = v_nbr.just_readAll();
    if(a_contentType)
    {
        QByteArray v_contentType = v_reply->rawHeader("Content-Type");
        qDebug() << "[v_contentType]" << v_contentType;
        (*a_contentType) = v_contentType;
    }
    delete v_reply;
    ////delete v_nbr;
    return v_response;
}

QByteArray BatchNetworkAccessManager::doPost(QNetworkRequest request, QByteArray postData)
{
    qDebug() << "doPost()" << request.url();
    QBuffer v_postBuffer;
    v_postBuffer.setBuffer(&postData);
    QNetworkReply *v_reply = this->post(request, &v_postBuffer);
    T_NetworkWaiter v_nbr(v_reply);
    v_nbr.wait();
    QByteArray v_response = v_nbr.just_readAll();
    delete v_reply;
    return v_response;
}

int BatchNetworkAccessManager::batDelete(QNetworkRequest request)
{
    qDebug() << "[BatchNetworkAccessManager::batDelete()]" << request.url();
    QNetworkReply *v_reply = this->deleteResource(request);
    T_NetworkWaiter v_nbr(v_reply);
    v_nbr.wait();
    QByteArray v_response = v_nbr.just_readAll();
    QVariant v_status_code = v_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
    qDebug() << "[BatchNetworkAccessManager::batDelete()] [v_response]" <<v_response;
    delete v_reply;
    return v_status_code.toInt();
}

int BatchNetworkAccessManager::batPost(QNetworkRequest request, QByteArray &postData, QByteArray &responsdData)
{
    qDebug() << "batPost()" << request.url();
    QBuffer v_postBuffer;
    v_postBuffer.setBuffer(&postData);
    QNetworkReply *v_reply = this->post(request, &v_postBuffer);
    T_NetworkWaiter v_nbr(v_reply);
    v_nbr.wait();
    QVariant v_status_code = v_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
    delete v_reply;
    return v_status_code.toInt();
}

QList<QByteArray> BatchNetworkAccessManager::doGetList(QList<QNetworkRequest> requestList)
{
    QList<T_NetworkWaiter *> v_seq_list;
    for(int i=0; i<requestList.size(); i++)
    {
        QNetworkReply *v_reply = this->get(requestList[i]);
        T_NetworkWaiter *v_seq = new T_NetworkWaiter(v_reply);
        v_seq_list.append(v_seq);
    }
    QList<QByteArray> v_result;
    for(int i=0; i<v_seq_list.size(); i++)
    {
        v_seq_list[i]->wait();
        v_result.append(v_seq_list[i]->just_readAll());
    }
    return v_result;
}
